//
//  WBEmoticonCollectionViewCell.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/23.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBEmoticonCollectionViewCell.h"
#import "WBEmoticonManager.h"
#import "WBEmoticonButton.h"
#import "WBEmoticonPopoView.h"

@interface WBEmoticonCollectionViewCell ()

@property(nonatomic ,strong) NSArray<WBEmoticonButton *> *arrayBtn;

@property(nonatomic ,strong) WBEmoticonPopoView *popoView;

@end

@implementation WBEmoticonCollectionViewCell

-(WBEmoticonPopoView *)popoView{

    if (nil == _popoView) {
        
        _popoView = [WBEmoticonPopoView emoticonPopoView];
    }
    
    return _popoView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{

    CGFloat btnW = kUISCRRENW / 7;
    CGFloat btnH = (self.bounds.size.height - 30) / 3;
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i < 20; i ++) {
        
        WBEmoticonButton *btn = [WBEmoticonButton buttonWithType:0];
        NSInteger row = i / 7;
        NSInteger col = i % 7;
        
        btn.frame = CGRectMake(col * btnW, row * btnH, btnW, btnH);
        //设置emoji大小
        btn.titleLabel.font = [UIFont systemFontOfSize:32];
        
        [btn addTarget:self action:@selector(emoticonBottonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:btn];
       
        [arrayM addObject:btn];
    }
    
    self.arrayBtn = arrayM.copy;
    //删除按钮
    UIButton *deleteBtn = [UIButton buttonWithType:0];
    [deleteBtn setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIControlStateNormal];
    [deleteBtn setImage:[UIImage imageNamed:@"compose_emotion_delete_highlighted"] forState:UIControlStateHighlighted];
    
    deleteBtn.frame = CGRectMake(kUISCRRENW - btnW, 2 * btnH, btnW, btnH);
    
    [deleteBtn addTarget:self action:@selector(deleteBottonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteBtn];
    
    //增加长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressBotton:)];
    
    [self addGestureRecognizer:longPress];
}

-(void)longPressBotton:(UILongPressGestureRecognizer *)longpress{

    CGPoint point = [longpress locationInView:self.contentView];
    //根据触摸点 找到表情
    WBEmoticonButton *btn = [self findEmoticonBtn:point];
    if (btn == nil) {
        
        [self.popoView removeFromSuperview];
        return;
    }
    //判断按钮是否是显示的 如果不是显示的就直接return
    if (btn.hidden) {
        [self.popoView removeFromSuperview];
        return;
    }
    
    switch (longpress.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
        {
            //显示popoView
            //添加到contentView上
            UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
            //转换左边  按钮需要将坐标转换到哪一个视图上
            //转换的坐标填什么有规律: 转换自己的坐标的时候就是用bounds 如果转换父视图的坐标的就用frame
            CGRect rect = [btn convertRect:btn.bounds toView:window];
            
            self.popoView.frame = CGRectMake(CGRectGetMidX(rect) - 32, CGRectGetMaxY(rect) - 91, 64, 91);
            //给表情按钮设置模型
            self.popoView.popoViewBotton.emoticon = btn.emoticon;
            [window addSubview:self.popoView];
        }
            break;
        default:
            [self.popoView removeFromSuperview];
            break;
    }
    
    
}

-(WBEmoticonButton *)findEmoticonBtn:(CGPoint)point {

    for (WBEmoticonButton *btn in self.arrayBtn) {
        
        if (CGRectContainsPoint(btn.frame, point)) {
            
            return btn;
        }
    }
    
    return nil;
}

-(void)setEmotionsArray:(NSArray<WBEmoticon *> *)emotionsArray{

    _emotionsArray = emotionsArray;
     //先设置全部为隐藏
    for (UIButton *btn in self.arrayBtn) {
        btn.hidden = YES;
    }
    
    
    [emotionsArray enumerateObjectsUsingBlock:^(WBEmoticon * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        self.arrayBtn[idx].hidden = NO;
        
        self.arrayBtn[idx].emoticon = obj;
        
    }];

}
#pragma mark : - 表情点击
-(void)emoticonBottonClick:(WBEmoticonButton *)btn{

    //如何从按钮中比较优雅获取一个模型
    //将表情模型存储到本地
    [[WBEmoticonManager shared] saveEmoticons:btn.emoticon];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"emoticonBottonClick" object:btn.emoticon];
}
#pragma mark : - 删除点击
-(void)deleteBottonClick:(UIButton *)btn{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"emoticonBottonClick" object:nil];
}

@end
